package jumpstart.test;

import java.net.URL;
import java.net.URLClassLoader;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import autoupdate.test.AutoUpdaterV2;

public class JumpStarter{

	private static Logger log;
	
	/**
	 * @param args
	 */
	public static void main(String[] args){
		System.out.println("Starting main.");
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(ClassNotFoundException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(InstantiationException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IllegalAccessException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(UnsupportedLookAndFeelException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		Get the System Classloader
        ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

        //Get the URLs
        URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
        System.out.println("Classpath:");
        for(int i=0; i< urls.length; i++)
        {
            System.out.println(urls[i].getFile());
        }
		
        
        
		//AutoUpd ater.patchAll();
		//int update = AutoUpdater.checkForUpdates(); 
		//System.out.println(update);
		//AutoUpdater.printFileList();
		//AutoUpdater.printUpdateSites();
		boolean areUpdates = AutoUpdaterV2.checkForUpdates();
		System.out.println(areUpdates);
		
		//If updates, update and restart
		if(areUpdates){
			AutoUpdaterV2.downloadUpdates();
			System.out.println("Shutting down main.");
		}
		//else run the program
		else{
			//Load Spring Context
			String[] contextPaths = new String[] { "classpath:app-context.xml" };
			ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(contextPaths);
			
			log = Logger.getLogger(JumpStarter.class);
			System.out.println("Debug: " + log.isDebugEnabled());
			
			log.debug("test");
			JFrame jFrame = (JFrame)appContext.getBean("jFrame");
			
			jFrame.setVisible(true);
			System.out.println("Running...");
		}
		
	}

}
